#!/usr/bin/perl -w
########################################################################
# script name:  dbi_query.pm
# Used for:	connect and execute on mysql databases
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/23    0.1     MDUMONT         initial
########################################################################
use strict;
use warnings;
use LoadConfig;
use DBI;
our $OPMMYCNF;
our $OPMMYGRP;
#######################################
# MAIN
#######################################
sub opm_mysql_fetchrow
{
	my $dsn = "DBI:mysql:;mysql_read_default_group=$OPMMYGRP;" . "mysql_read_default_file=$OPMMYCNF";
	my $DBIconnect = DBI->connect($dsn) or die "Unable to connect: $DBI::errstr\n";
	my $s_query_handle = $DBIconnect->prepare($_[0]);
	$s_query_handle->execute();
	my @row = $s_query_handle->fetchrow_array;
	#$DBIconnect->disconnect;
	return (@row);
}
sub opm_mysql_list_tables{
	my $dsn = "DBI:mysql:;mysql_read_default_group=$OPMMYGRP;" . "mysql_read_default_file=$OPMMYCNF";
	my $DBIconnect = DBI->connect($dsn) or die "Unable to connect: $DBI::errstr\n";
	my @a_DbTables = $DBIconnect->tables();
	$DBIconnect->disconnect;
	return (@a_DbTables);
}
sub opm_mysql_list_columns{
	my $dsn = "DBI:mysql:;mysql_read_default_group=$OPMMYGRP;" . "mysql_read_default_file=$OPMMYCNF";
	my $DBIconnect = DBI->connect($dsn) or die "Unable to connect: $DBI::errstr\n";
	my $query = qq/select column_name from information_schema.columns where table_name='$_[0]'/;
	my $s_query_handle = $DBIconnect->prepare($query);
	$s_query_handle->execute();
	my @a_columns;
	while (my $tmp = $s_query_handle->fetchrow){
		push(@a_columns, $tmp);
	}
	$s_query_handle->finish();
	$DBIconnect->disconnect;
	return (@a_columns);
}
